package cn.zwxky.android.user.Utils;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import cn.zwxky.android.user.R;


/**
 * Created by Administrator on 2016/6/26 0026.
 */
public abstract class PopWindowManager2 {

    private PopupWindow popupWindow;

    public PopWindowManager2(final Activity activity, int resource) {
        View contentView = LayoutInflater.from(activity)
                .inflate(resource, null);
        this.popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.popupWindow.setFocusable(true);
        this.popupWindow.setAnimationStyle(R.style.anim_popup_dir);
//		backgroundAlpha(activity, 0.5f);
        this.popupWindow.setBackgroundDrawable(new ColorDrawable());
        this.popupWindow.setOutsideTouchable(true);
        popupWindow
                .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                backgroundAlpha(activity, 1f);
            }
        });

        onViewLister(contentView, this);

    }

    /**
     * 默认显示在下�?
     *
     * @author zgscwjm
     * @time 2015�?10�?28�?-上午10:40:57
     * @param view
     */
    public void showWindows(View view) {
        this.popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }
    /**
     * 默认显示在下�?
     *
     * @author zgscwjm
     * @time 2015�?10�?28�?-上午10:40:57
     * @param view
     */
    public void showWindowsCenter(View view) {
        this.popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
    }
    public void showWindowsCenter2(View view) {
        this.popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        this.popupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
    }

    public void showWindowsTop(View view) {
		//this.popupWindow.showAtLocation(view, Gravity.TOP,0,0);
       this.popupWindow.showAsDropDown(view, Gravity.TOP,0,0);

    }
    public void showWindowsRight(View view) {
        this.popupWindow.showAsDropDown(view, Gravity.RIGHT,0,0);

    }

    /**
     * �Զ�����ʾdialog
     * @author nonsense
     * @param anchor
     * @param x
     * @param y
     */
    public void showWindows(View anchor, int x, int y) {
        this.popupWindow.showAsDropDown(anchor, x, y);
    }
    /**
     * 自定义显示地�?
     *
     * @author zgscwjm
     * @time 2015�?10�?29�?-上午10:52:14
     * @param view
     *            视图
     * @param gravity
     *            Gravity
     * @param x
     *            偏移量x
     * @param y
     *            偏移量y
     */
    public void showWindows(View view, int gravity, int x, int y) {
        this.popupWindow.showAtLocation(view, gravity, x, y);
    }

    /**
     * 隐藏窗体
     *
     * @author zgscwjm
     * @time 2015�?10�?28�?-下午1:16:38
     */
    public void hideWindow() {
        this.popupWindow.dismiss();
    }

    /**
     * 设置背景透明�?
     *
     * @author zgscwjm
     * @time 2015�?10�?28�?-上午10:25:52
     * @param activity
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        activity.getWindow().setAttributes(lp);
    }

    /**
     * 这里注册控件以及监听控件的事�?
     *
     * @author zgscwjm
     * @time 2015�?10�?28�?-上午10:58:12
     */
    abstract public void onViewLister(View contentView, PopWindowManager2 manager);

    public Boolean isshow(){
        return popupWindow.isShowing();
    }
    public  void setbackgroundgray(Activity activity)
    {
        backgroundAlpha(activity, 0.5f);
    }
    public PopupWindow getPopupWindow(){
        return this.popupWindow;
    }
    public  void setbackgroundgray2()
    {
        this.popupWindow.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
    }


}
